package com.blog.web.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.blog.common.core.domain.R;
import com.blog.common.core.web.domain.AjaxResult;
import com.blog.common.core.web.page.TableDataInfo;
import com.blog.common.domain.dto.CompiledArticleDto;
import com.blog.common.domain.dto.ConditionDto;
import com.blog.common.domain.entity.Article;
import com.blog.common.domain.vo.*;
import com.blog.web.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 文章 控制器
 * </p>
 *
 * @author Valerie
 * @date 2023/2/19
 **/
@RestController
@RequiredArgsConstructor
@Api("文章模块")
public class ArticleController {

    private final ArticleService articleService;

    /**
     * 查看首页文章
     *
     * @return {@link R<TableDataInfo<ArticleHomeVo>>}
     */
    @GetMapping("/articles")
    @ApiOperation("查看首页文章")
    public R<TableDataInfo<ArticleHomeVo>> listArticles(){
        return R.ok(articleService.listArticles());
    }

    /**
     * 查看首页文章
     *
     * @return {@link R<TableDataInfo<ArticleSearchVo>>}
     */
    @GetMapping("/articles/search")
    @ApiOperation("查看首页文章")
    public R<TableDataInfo<ArticleSearchVo>> listArticlesBySearch(@RequestParam("keywords") Serializable keywords, @RequestParam(value = "pageType", defaultValue = "0") Serializable pageType){
        return R.ok(articleService.listArticlesBySearch(keywords, pageType));
    }

    /**
     * 查看文章归档
     * @param
     * @return {@link AjaxResult}
     */
    @GetMapping("/articles/archives")
    @ApiOperation("查看文章归档")
    public R<TableDataInfo<ArticlePreviewVoList>> listArchives(){
        return R.ok(articleService.listArchives());
    }

    /**
     * 根据条件查询文章
     * @param
     * @return {@link R<TableDataInfo<ArticlePreviewVoList>>}
     */
    @GetMapping("/articles/condition")
    @ApiOperation("根据条件查询文章")
    public R<TableDataInfo<ArticlePreviewVoList>> listArticlesByCondition(ConditionDto condition){
        return R.ok(articleService.listArticlesByCondition(condition));
    }

    /**
     * 查询最新文章
     * @param
     *
     * @return {@link R<List<ArticleVo>>}
     */
    @GetMapping("/articles/latest")
    @ApiOperation("查询最新文章")
    public R<List<ArticleVo>> listArticlesByCreateTime(){
        return R.ok(articleService.listArticlesByCreateTime());
    }

    /**
     * 根据id查看文章
     * @param articleId 文章id
     * @return {@link R<ArticleVo>}
     */

    @GetMapping("/article")
    @ApiOperation("根据id查看文章")
    public R<ArticleVo> getArticleById(@RequestParam("articleId") Serializable articleId){
        return R.ok(articleService.getArticleById(articleId));
    }

    /**
     * 点赞文章
     *
     * @param articleId 文章id
     * @return {@link R<Void>}
     */
    @ApiOperation(value = "点赞文章")
    @ApiImplicitParam(name = "articleId", value = "文章id", required = true, dataType = "Integer")
    @PostMapping("/articles/{articleId}/like")
    public R<Void> saveArticleLike(@PathVariable("articleId") Serializable articleId) {
        // todo
        articleService.saveArticleLike(articleId);
        return R.ok();
    }

    /**
     * 获取归档时间
     *
     * @param pageSize 页数
     * @return {@link R<List<ArchiveSideVo>>}
     */
    @GetMapping("/archive/side")
    @ApiOperation(value = "获取归档时间")
    public R<List<ArchiveSideVo>> listArchiveDate(@RequestParam(value = "pageSize", required = false) Serializable pageSize){
        return R.ok(articleService.listArchiveDate(pageSize));
    }

    /**
     * 获取归档时间
     *
     * @param archiveDate 页数
     * @return {@link R<TableDataInfo<ArticlePreviewVoList>>}
     */
    @GetMapping("/articles/archives/date")
    @ApiOperation(value = "获取时间获取文章")
    public R<TableDataInfo<ArticlePreviewVoList>> listArchiveByDate(@RequestParam("archiveDate") Serializable archiveDate){
        return R.ok(articleService.listArchiveByDate(archiveDate));
    }

    /**
     * 获取首页推荐文章
     *
     * @return {@link R<List<ArticleRecommendVo>>}
     */
    @GetMapping("/article/recommend")
    @ApiModelProperty()
    public R<List<ArticleRecommendVo>> listArticleByRecommend(){
        return R.ok(articleService.listArticleByRecommend());
    }

    /**
     * 获取归档时间
     *
     * @return {@link R<Void>}
     */
    @GetMapping("/articles/date")
    @ApiOperation(value = "获取时间获取文章")
    public R<Void> getArticleByDate(){
        return R.ok();
    }

    /**
     * 获取归档时间
     *
     * @return {@link R<List<ArchivePreviewVoList>>}
     */
    @GetMapping("/articles/archives/yearsAndMonth")
    @ApiOperation(value = "获取时间获取文章-》月份")
    public R<List<ArchivePreviewVoList>> getArticleByYearAndMonth(){
        return R.ok(articleService.getArticleByYearAndMonth());
    }

    /**
     * 预编译文章
     *
     * @param compiledArticleDto 内容
     * @return {@link R<Void>}
     */
    @PutMapping("/articles/updateCompiledArticleContent")
    @ApiOperation(value = "预编译文章")
    public R<Void> updateCompiledArticleContent(@RequestBody CompiledArticleDto compiledArticleDto){
        articleService.update(new LambdaUpdateWrapper<Article>()
                .eq(Article::getId, compiledArticleDto.getId())
                .set(Article::getCompiledArticleContent, compiledArticleDto.getCompiledArticleContent()));
        return R.ok();
    }

}
